﻿using AutoMapper;
using Compass.Wasm.Shared;
using Compass.Wasm.Shared.Categories;

namespace Compass.Wasm.Server.Services.Categories;

public interface IDrawingNumberService : IBaseService<DrawingNumberDto>
{
    Task<ApiResponse<IEnumerable<DrawingNumberDto>>> GetAllByKeywordAsync(string keyword);
}

public class DrawingNumberService: IDrawingNumberService
{
    private readonly CategoryDbContext _dbContext;
    private readonly ICategoryRepository _repository;
    private readonly IMapper _mapper;
    public DrawingNumberService(CategoryDbContext dbContext, ICategoryRepository repository, IMapper mapper)
    {
        _dbContext = dbContext;
        _repository = repository;
        _mapper = mapper;
    }
    #region 基本增删改查
    public async Task<ApiResponse<IEnumerable<DrawingNumberDto>>> GetAllAsync()
    {
        try
        {
            var models = await _repository.GetDrawingNumbersAsync();
            var dtos = await _mapper.ProjectTo<DrawingNumberDto>(models).OrderBy(x => x.Name).ToListAsync();
            return new ApiResponse<IEnumerable<DrawingNumberDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<DrawingNumberDto>> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<DrawingNumberDto>> GetSingleAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetDrawingNumberByIdAsync(id);
            if (model == null) return new ApiResponse<DrawingNumberDto> { Status = false, Message = "查询数据失败" };
            var dto = _mapper.Map<DrawingNumberDto>(model);
            return new ApiResponse<DrawingNumberDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<DrawingNumberDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<DrawingNumberDto>> AddAsync(DrawingNumberDto dto)
    {
        try
        {
            var model = new DrawingNumber(Guid.NewGuid(), dto.Name, dto.Description, dto.Type, dto.Remarks, dto.ImageUrl);
            await _dbContext.DrawingNumbers.AddAsync(model);
            dto.Id = model.Id;
            return new ApiResponse<DrawingNumberDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<DrawingNumberDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<DrawingNumberDto>> UpdateAsync(Guid id, DrawingNumberDto dto)
    {
        try
        {
            var model = await _repository.GetDrawingNumberByIdAsync(id);
            if (model == null) return new ApiResponse<DrawingNumberDto> { Status = false, Message = "更新数据失败" };
            model.Update(dto);
            return new ApiResponse<DrawingNumberDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<DrawingNumberDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<DrawingNumberDto>> DeleteAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetDrawingNumberByIdAsync(id);
            if (model == null) return new ApiResponse<DrawingNumberDto> { Status = false, Message = "删除数据失败" };
            model.SoftDelete();//软删除
            return new ApiResponse<DrawingNumberDto> { Status = true };
        }
        catch (Exception e)
        {
            return new ApiResponse<DrawingNumberDto> { Status = false, Message = e.Message };
        }
    }
    #endregion


    #region 扩展查询
    public async Task<ApiResponse<IEnumerable<DrawingNumberDto>>> GetAllByKeywordAsync(string keyword)
    {
        try
        {
            var models = await _repository.GetDrawingNumbersByKeywordAsync(keyword);
            var dtos = await _mapper.ProjectTo<DrawingNumberDto>(models).OrderByDescending(x => x.Name).ToListAsync();
            return new ApiResponse<IEnumerable<DrawingNumberDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<DrawingNumberDto>> { Status = false, Message = e.Message };
        }
    }
    #endregion
}